package com.yuntsg.nnsfcp.test.pdf;

import com.aspose.words.*;
import lombok.SneakyThrows;

import java.util.Iterator;

/**
 * @Author: 葛成瑞
 * @Date: 2024/9/23 16:29
 */
public class Asposewordtable {
    @SneakyThrows
    public static void main(String[] args) {
        String pdfPathWord = "D:/Users/55419/Desktop/国自然原始文件222.docx";
        Document doc = new Document(pdfPathWord);

        NodeCollection childNodes = doc.getChildNodes();
        SectionCollection sections = doc.getSections();
        for (Section section : sections) {
            Body body = section.getBody();
            TableCollection tables = body.getTables();
            for (Table table : tables) {
                RowCollection rows = table.getRows();
                for (Row row : rows) {
                    CellCollection cells = row.getCells();
                    for (Cell cell : cells) {
                        String text = cell.getText();
                        System.out.println(text);
                    }
                }
            }
        }
        Iterator iterator = childNodes.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            if (next instanceof Table) {
                RowCollection rows = ((Table) next).getRows();
                for (Row row : rows) {
                    CellCollection cells = row.getCells();
                    for (Cell cell : cells) {
                        String text = cell.getText();
                        System.out.println(text);
                    }
                }
            }
        }
        int pageCount = doc.getPageCount();


        NodeCollection children = doc.getChildNodes();
        for (Node child : (Iterable<Node>) children) {
            System.out.println(child.getNodeType());
            // Paragraph may contain children of various types such as runs, shapes and so on.
            if (child.getNodeType() == NodeType.TABLE) {
                // Say we found the node that we want, do something useful.
                RowCollection rows = ((Table) child).getRows();
                for (Row row : rows) {
                    CellCollection cells = row.getCells();
                    for (Cell cell : cells) {
                        String text = cell.getText();
                        System.out.println(text);
                    }
                }

            }
        }

    }
}
